package cn.buk.tms.eotms.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 账单中的机票改签订单内容
 */
@NamedQueries({
        @NamedQuery(
                name = "BillChangeOrder.searchMonthly",
                query = "select new cn.buk.tms.eotms.report.dto.ReportFlightChangeDto(o.balanceCode, o.ticketNo, " +
                        "b.opDate, o.psgName, o.remark, " +
                        "o.parvalue, o.tax, o.airChangeCharge, o.serviceCharge," +
                        "o.newTicketNo, o.intlTicket, " +
                        "b.op1, b.id, b.bePaid) from BillChangeOrder o left join BillInfo b on o.billNum = b.billNum " +
                        " where o.enterpriseId = :enterpriseId and o.customerId = :customerId and b.opDate >= :beginDate and b.opDate <= :endDate " +
                        " order by b.opDate, o.balanceCode, o.ticketNo"
        ),
        //以上查询语句，因为散客的customer_id字段为空，会导致散客的结果出不来，需要改进
})
@Entity
@Table(name = "bill_flt_change_order",
        uniqueConstraints=@UniqueConstraint(name="uk_bill_flt_change_order", columnNames={"enterprise_id", "balance_code", "ticket_no", "change_count"}))
public class BillChangeOrder {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;

    /**
     * 创建改签单的企业
     */
    @Column(name = "enterprise_id")
    private int enterpriseId;

    @Column(name = "balance_code", length = 3)
    private String balanceCode;

    /**
     * 要改签的机票的票号
     */
    @Column(name = "ticket_no", length = 10)
    private String ticketNo;

    /**
     * 改签次数
     */
    @Column(name = "change_count")
    private int changeCount;

    /**
     * 账单号
     **/
    @Column(length = 32, nullable = false)
    private String billNum;



    /**
     * 改签单单号
     */
    @Column(name = "change_order_no", length = 10)
    private String changeOrderNo;

    /**
     * 改签单的生成日期（用于结算单报表）
     */
    @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
    @Temporal(TemporalType.DATE)
    @Column(name = "order_date")
    private Date orderDate;

    /**
     * 订单号(该改签单来自哪个订单）
     */
    @Column(name = "order_no", length = 10)
    private String orderNo;

    @Column(name = "order_id")
    private Integer orderId;


    /**
     * 客户类型
     * 0 - 代表散客
     * 1 - 代表企业客户
     */
    @Column(name="customer_type")
    private int customerType;

    /**
     * 冗余字段，应该和Order.customer值保持一致
     */
    @ManyToOne
    @JoinColumn(name="customer_id", foreignKey = @ForeignKey(name = "fk_bill_change_customer"))
    private Customer customer;

    @Column(name = "customer_id", insertable = false, updatable = false)
    private Integer customerId;

    /**
     * 成本中心
     */
    @Column(name = "cost_center", length = 50)
    private String costCenter;


    /**
     * 旅客姓名
     */
    @Column(name = "psg_name", length = 50)
    private String psgName;

    @Column(name = "id_no", length = 20)
    private String idNo;


    private Double parvalue;

    private Double tax;


    /**
     * 企业收的改签服务费
     */
    @Column(name = "service_charge")
    private double serviceCharge;

    /**
     * 航司收的改签费
     */
    @Column(name = "air_change_fee")
    private double airChangeCharge;


    private int status;

    /**
     * 改签类型
     * 0-升舱
     * 1-更改
     * 2-签转
     */
    @Column(name = "change_type")
    private int changeType;

    /**
     * 操作员：下订单的操作员
     */
    @Column(length = 32)
    private String operator;

    /**
     * 新票号(如果有）
     */
    @Column(name = "new_ticket_no", length = 14)
    private String newTicketNo;

    @Column(length = 2000)
    private String remark;

    /**
     * 是否国际票改签
     * 0-否
     * 1-是
     */
    @Column(name = "intl_ticket")
    private int intlTicket;

    /**
     * 该账单客户的付款方式
     * 1-现金；
     * 2-信用卡；
     * 4-支票；
     * 8-记账(默认）
     */
    @Column(name = "pay_type")
    private Integer payType;

    /**
     * 是否已付款
     */
    @Column(name = "be_paid", length = 1)
    private String bePaid;

    @Transient
    private List<FlightInfo> flights;


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_time", insertable = false, updatable = false)
    private Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_update", insertable = false)
    private Date lastUpdate;



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        if (remark != null && remark.length() > 1800) {
            remark = remark.trim().substring(0, 1800);
        }
        this.remark = remark;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }


    public String getTicketNo() {
        return ticketNo;
    }

    public void setTicketNo(String ticketNo) {
        this.ticketNo = ticketNo;
    }

    public String getPsgName() {
        return psgName;
    }

    public void setPsgName(String psgName) {
        this.psgName = psgName;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String getBalanceCode() {
        return balanceCode;
    }

    public void setBalanceCode(String balanceCode) {
        this.balanceCode = balanceCode;
    }

    public double getServiceCharge() {
        return serviceCharge;
    }

    public void setServiceCharge(double serviceCharge) {
        this.serviceCharge = serviceCharge;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public String getOperator() {
        return operator;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public Integer getOrderId() {
        return orderId == null ? 0 : orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public Double getParvalue() {
        return parvalue == null ? 0: parvalue;
    }

    public void setParvalue(double parvalue) {
        this.parvalue = parvalue;
    }

    public Double getTax() {
        return tax == null ? 0: tax;
    }

    public void setTax(double tax) {
        this.tax = tax;
    }

    public String getChangeOrderNo() {
        return changeOrderNo;
    }

    public void setChangeOrderNo(String changeOrderNo) {
        this.changeOrderNo = changeOrderNo;
    }

    public double getAirChangeCharge() {
        return airChangeCharge;
    }

    public void setAirChangeCharge(double airChangeCharge) {
        this.airChangeCharge = airChangeCharge;
    }

    public int getCustomerType() {
        return customerType;
    }

    public void setCustomerType(int customerType) {
        this.customerType = customerType;
    }

    public int getIntlTicket() {
        return intlTicket;
    }

    public void setIntlTicket(int intlTicket) {
        this.intlTicket = intlTicket;
    }

    public int getChangeType() {
        return changeType;
    }

    public void setChangeType(int changeType) {
        this.changeType = changeType;
    }


    public String getNewTicketNo() {
        return newTicketNo;
    }

    public void setNewTicketNo(String newTicketNo) {
        this.newTicketNo = newTicketNo;
    }

    public String getCostCenter() {
        return costCenter;
    }

    public void setCostCenter(String costCenter) {
        this.costCenter = costCenter;
    }

    public Integer getPayType() {
        return payType == null ? 8 : payType;
    }

    public void setPayType(Integer payType) {
        this.payType = payType;
    }

    public String getBillNum() {
        return billNum;
    }

    public void setBillNum(String billNum) {
        this.billNum = billNum;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public List<FlightInfo> getFlights() {
        if (flights == null) {
          flights = new ArrayList<>();
        }
        return flights;
    }

    public void setFlights(List<FlightInfo> flights) {
        this.flights = flights;
    }


    public String getFlightNo() {
        return getFlights().size() == 0 ? "" : getFlights().get(0).getFlightNo();
    }

    public String getSubclass() {
        return getFlights().size() == 0 ? "" : getFlights().get(0).getSubclass();
    }

    public String getDport() {
        return getFlights().size() == 0 ? "" : getFlights().get(0).getDepartureAirport();
    }

    public String getAport() {
        return getFlights().size() == 0 ? "" : getFlights().get(0).getArrivalAirport();
    }

    public String getDterm() {
        return getFlights().size() == 0 ? "" : getFlights().get(0).getDepartureTerminal();
    }

    public String getAterm() {
        return getFlights().size() == 0 ? "" : getFlights().get(0).getArrivalTerminal();
    }

    public String getDdate() {
        return getFlights().size() == 0 ? "" : getFlights().get(0).getDepartureDate();
    }

    public String getDtime() {
        return getFlights().size() == 0 ? "" : getFlights().get(0).getDepartureTime();
    }

    public String getAtime() {
        return getFlights().size() == 0 ? "" : getFlights().get(0).getArrivalTime();
    }

    public String getBePaid() {
        return bePaid;
    }

    public void setBePaid(String bePaid) {
        this.bePaid = bePaid;
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    public int getChangeCount() {
        return changeCount;
    }

    public void setChangeCount(int changeCount) {
        this.changeCount = changeCount;
    }
}
